package com.example.a14110100409.yuejiandemo.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.a14110100409.yuejiandemo.R;
import com.example.a14110100409.yuejiandemo.javabean.OrderInfo;
import com.example.a14110100409.yuejiandemo.model.UserModel;

import it.sephiroth.android.library.picasso.Picasso;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class OrderInfoActivity extends AppCompatActivity {
    private TextView orderinfo_name,orderinfo_time,orderinfo_place,orderinfo_adultnum,orderinfo_paytype,orderinfo_vtype,payprice,orderinfo_phone,danjia;
    private ImageView  orderinfo_pic,orderinfo_back;
    private Button orderinfo_tuikuan;
    private String order_id,order_type,str_buynum,user_name;
    private OrderInfo orderInfo;
    private Double price,allprice,buynum;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_info);
        initViews();
        initEvents();
        showData();
    }
    private void initViews(){
        orderinfo_name = (TextView) findViewById(R.id.orderinfo_name);
        orderinfo_time = (TextView) findViewById(R.id.orderinfo_time);
        orderinfo_place = (TextView) findViewById(R.id.orderinfo_place);
        orderinfo_adultnum = (TextView) findViewById(R.id.orderinfo_adultnum);
        orderinfo_paytype = (TextView) findViewById(R.id.orderinfo_paytype);
        orderinfo_vtype = (TextView) findViewById(R.id.orderinfo_vtype);
        orderinfo_phone = (TextView) findViewById(R.id.orderinfo_phone);
        danjia = (TextView) findViewById(R.id.orderinfo_danjia);
        payprice = (TextView) findViewById(R.id.payprice);
        orderinfo_pic = (ImageView) findViewById(R.id.orderinfo_pic);
        orderinfo_back = (ImageView) findViewById(R.id.orderinfo_back);
        orderinfo_tuikuan = (Button) findViewById(R.id.orderinfo_tuikuan);
        user_name = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getString("username","0");
    }
    private void initEvents(){
        Intent intent = getIntent();
        order_id=intent.getStringExtra("order_id");
        order_type = intent.getStringExtra("order_type");
        price = Double.valueOf(intent.getStringExtra("price"));
        orderinfo_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        orderinfo_tuikuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void showData(){
        UserModel userModel = new UserModel();
        Call<OrderInfo> call = userModel.getOrderInfo(order_id,order_type);
        call.enqueue(new Callback<OrderInfo>() {
            @Override
            public void onResponse(Call<OrderInfo> call, Response<OrderInfo> response) {
                orderInfo = response.body();
                orderinfo_name.setText(orderInfo.getName());
                orderinfo_place.setText(orderInfo.getPlace());
                orderinfo_time.setText(orderInfo.getTraintime());
                orderinfo_adultnum.setText(orderInfo.getAdultnum());
                orderinfo_phone.setText(user_name);
                danjia.setText(orderInfo.getPrice());
                str_buynum = orderInfo.getAdultnum().toString();
                buynum = Double.parseDouble(str_buynum);
                allprice = price*buynum;
                payprice.setText(allprice.toString()+"元");
                if (orderInfo.getPaytype()=="1"){
                    orderinfo_paytype.setText("支付宝支付");
                }
                if (orderInfo.getPaytype()=="2"){
                    orderinfo_paytype.setText("微信支付");
                }
                if (orderInfo.getVstate()=="0"){
                    orderinfo_vtype.setText("未参加");
                }
                if (orderInfo.getVstate()=="1"){
                    orderinfo_vtype.setText("已完成");
                }
                if (orderInfo.getThumb()==null){
                    orderinfo_pic.setImageResource(R.drawable.tx);
                }
                else {
                    String address="http://172.24.10.175/workout/Uploads/"+orderInfo.getThumb();
                    Picasso.with(mContext).load(address).into(orderinfo_pic);
                }
            }

            @Override
            public void onFailure(Call<OrderInfo> call, Throwable t) {

            }
        });
    }
}
